//
//  GridControllerUnitTest.m
//  iD20
//
//  Created by Andrew Spina on 5/4/10.
//  Copyright 2010 STScI. All rights reserved.
//

#import "GridControllerUnitTests.h"


@implementation GridControllerUnitTests

@synthesize controller;

- (void) assertPoint: (Point2d *) point hasToken: (NSString *) token {
	STAssertTrue([controller isTokenAtPoint:point], @"Expected token %s at %@", token, point);
	NSString * tokenText = [controller tokenAtPoint:point].name;
	STAssertTrue([token isEqualToString:tokenText], @"Should have found %@", tokenText);
	NSString * uiText = [controller cellForPoint:point].token;
	STAssertTrue([token isEqualToString:uiText], @"Should have found %@ on cell UI at %@ but found %@", token, point, uiText);
}

- (void) assertPointIsNotToken: (Point2d *) point {
	STAssertFalse([controller isTokenAtPoint:point], @"Didn't expect token at %@ but found %@", point, [controller tokenAtPoint:point]);
}

//- (void) testGridControllerSetup {
//	NSURL *model = [NSURL fileURLWithPath:@"build/Debug-iphonesimulator/iD20_Unit_Tests.octest/iD20.mom"];
//	NSManagedObjectModel* managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:model] retain]; 
//    NSPersistentStoreCoordinator * persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: managedObjectModel];
//	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
//	NSURL *storeUrl = [NSURL fileURLWithPath: [basePath stringByAppendingPathComponent: @"iD20.sqlite"]];
//	NSError *error;
//	[persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error];
//	NSManagedObjectContext* context = [[NSManagedObjectContext alloc] init];
//	[context setPersistentStoreCoordinator: persistentStoreCoordinator];
//	Token *token = (Token *)[NSEntityDescription insertNewObjectForEntityForName:@"Token" inManagedObjectContext:context];
//	
////	controller = [[GridController alloc] retain];
////	controller.managedObjectContext = context;
////	[context 
//}

- (void) testBasicTokenFunctions {
	
	controller = [[GridController alloc] retain];
	//TODO make this configuration correct. (self below should be an App delegate.)
	NSURL *model = [NSURL fileURLWithPath:@"build/Debug-iphonesimulator/iD20_Unit_Tests.octest/iD20.mom"];
	NSManagedObjectModel* managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:model] retain]; 
    NSPersistentStoreCoordinator * persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: managedObjectModel];
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
	NSURL *storeUrl = [NSURL fileURLWithPath: [basePath stringByAppendingPathComponent: @"iD20.sqlite"]];
	NSError *error;
	[persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error];
	NSManagedObjectContext* context = [[NSManagedObjectContext alloc] init];
	[context setPersistentStoreCoordinator: persistentStoreCoordinator];
	[NSEntityDescription insertNewObjectForEntityForName:@"Token" inManagedObjectContext:context];
	
//	controller = [[GridController alloc] retain];
//	controller.managedObjectContext = context;
//	[context 
}

- (void) testBasicTokenFunctions {
	
	controller = [[GridController alloc] retain];
	
	NSURL *model = [NSURL fileURLWithPath:@"build/Debug-iphonesimulator/iD20_Unit_Tests.octest/iD20.mom"];
	NSManagedObjectModel* managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:model] retain]; 
    NSPersistentStoreCoordinator * persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: managedObjectModel];
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
	NSURL *storeUrl = [NSURL fileURLWithPath: [basePath stringByAppendingPathComponent: @"iD20.sqlite"]];
	NSError *error;
	[persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error];
	NSManagedObjectContext* context = [[NSManagedObjectContext alloc] init];
	[context setPersistentStoreCoordinator: persistentStoreCoordinator];
	
	controller.managedObjectContext = context;
	//controller = appDelegate.myViewController;
	[controller viewDidLoad];
	// We'll change this once we can dynamically specify a map.
	[controller loadMapData];
//	NSManagedObjectContext *context = [appDelegate managedObjectContext];
//	controller.managedObjectContext = context;
	STAssertNil(controller.activeTokenLocation, @"Shouldn't start with anything selected.");
	Point2d* tokenLocation = [[Point2d createPointAtX:0 andY:0] retain];
	[self assertPoint:tokenLocation hasToken:@"RobToken.png"];
	[controller tapCell:tokenLocation];
	STAssertTrue([controller.activeTokenLocation equalsPoint:tokenLocation], @"Contoller should have selected %@ but selected %@", tokenLocation, controller.activeTokenLocation);
	// Shouldn't have moved.
	[self assertPoint:tokenLocation hasToken:@"RobToken.png"];
	Point2d *destination = [[Point2d createPointAtX:1 andY:1] retain];
	[self assertPointIsNotToken: destination];
	STAssertTrue([controller cellForPoint:tokenLocation].highlighted, @"Origin should be highlighted.");
	STAssertFalse([controller cellForPoint:destination].highlighted, @"Destination was highlighted, but wasn't the main button.");
	// TODO Should we be testing the actual colors? There should be a nicer way of verifying the zone is being shown...
	STAssertEqualObjects([controller cellForPoint:tokenLocation].bgcolor, [UIColor colorWithRed:.6 green:.6 blue:.8 alpha:.8], @"BG color should be clear for tapped target.");
	STAssertEqualObjects([controller cellForPoint:destination].bgcolor, [UIColor colorWithRed:0 green:.2 blue:.05 alpha:.5], @"Destination should be pale green.");
	// Complete moving the token to the destination.
	[controller tapCell:destination];
	[self assertPointIsNotToken:tokenLocation];
	[self assertPoint:destination hasToken:@"RobToken.png"];
	STAssertNil(controller.activeTokenLocation, @"Controller should have selected nil but selected %@", controller.activeTokenLocation);
	// Test cancel behavior.
	[controller tapCell: destination];
	STAssertTrue([controller cellForPoint:destination].highlighted, @"Origin should be highlighted.");
	STAssertTrue([controller.activeTokenLocation equalsPoint:destination], @"Contoller should have selected %@ but selected %@", destination, controller.activeTokenLocation);
	[controller tapCell: destination];
	STAssertFalse([controller cellForPoint:destination].highlighted, @"Movement should be cancelled.");
	STAssertNil(controller.activeTokenLocation, @"Controller should have selected nil but selected %@", controller.activeTokenLocation);
	
	//TODO add tests for speed. Verify that you can't move further than you're allowed speed. Check boundaries for that.

	
	[destination release];
	[tokenLocation release];
	controller = nil;
}

@end
